package translate;

import java.util.List;
import asa.PostBucket;
import forum.ForumPost;

public abstract class Translate extends Thread {
	private PostBucket inBucket;
	private PostBucket translateBucket;

	public Translate(String name, PostBucket inBucket,
			PostBucket translateBucket) {
		super("Translate-" + name);
		this.inBucket = inBucket;
		this.translateBucket = translateBucket;
	}

	public abstract List<ForumPost> translate(List<ForumPost> posts) throws Exception;

	public void run() {
		while (!inBucket.finished() || inBucket.size() > 0) {
			try {
				List<ForumPost> posts = inBucket.getPost();
				if (posts == null)
					break;
				
				List<ForumPost> translatedPosts = translate(posts);
				
				if (translatedPosts.size() > 0)
					translateBucket.addPost(translatedPosts);
				
			} catch (InterruptedException ie) {
				break;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		translateBucket.finish();
		System.err.println("Translation thread finished!");
	}
}
